<?php

/**
 * This is the model class for table "{{Categrory}}".
 *
 * The followings are the available columns in table '{{Categrory}}':
 * @property integer $cat_id
 * @property integer $slug
 * @property integer $cat_name
 */
class Categrory extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Categrory the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{Categrory}}';
    }

    public function getUrl() {
        return Yii::app()->createUrl('categrory/cat', array('url' => $this->cat_id . '-' . $this->slug));
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('slug, cat_name', 'required'),
            array('order_by,slug,cat_bane,cat_id', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('cat_id, slug, cat_name', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'parent' => array(self::HAS_MANY, 'CatRelation', 'child_id',
                'condition' => 'parent.parent_id=0'),
//            'child'=>array(self::HAS_MANY,'CatRelation','child_id',
//                    'condition'=>'child.parent_id='.$this->cat_id),
        );
    }

    public static function findChild($id) {
        $child = CatRelation::model()->findAll('parent_id=' . $id);


        return ($child == Null) ? Null : $child;
    }

    public function echoSubmenu() {
        if (CatRelation::model()->findAll('parent_id=' . $this->cat_id) == Null) {
            return '';
        } else {

            $str = '<div class="submenu"><ul>';
            foreach (CatRelation::model()->findAll('parent_id=' . $this->cat_id) as $con_cat) {
                $str.='<li>';
                $str.='<a  href="' . $con_cat->cat->url . '">' . $con_cat->cat->cat_name . '</a>';

                $str.=$con_cat->cat->echoSubmenu();
                $str.='</li>';
            }
            $str.='</ul></div>';
            return $str;
        }
    }

    public static function test() {
        return array(1 => '2', 2 => '3');
    }

    public function getCatNamebyId() {
        return (isset($this->cat_name)) ? $this->cat_name : 'no cat';
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'cat_id' => 'Cat',
            'slug' => 'Tên liên kết',
            'cat_name' => 'Tên thư mục',
            'order_by' => 'Sắp xếp',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('cat_id', $this->cat_id);
        $criteria->compare('slug', $this->slug);
        $criteria->compare('cat_name', $this->cat_name);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function getCatlist() {
        $model = Categrory::model()->findAll();

        $arr = array();
        $arr[0] = 'No parent';

        foreach ($model as $v) {
            $arr[$v->cat_id] = $v->cat_name;
        }

        return $arr;
    }

}